/**
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
*
* MeetRoomManageService
* 会议室管理
*
* 修改纪录
* 2020-10-26 版本：1.0 wanghongwei 创建。
* @version 版本：1.0
* @author 作者：wanghongwei
* 创建日期：2020-10-26
*/
package com.topscomm.meet.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tencentcloudapi.wemeet.common.constants.InstanceEnum;
import com.tencentcloudapi.wemeet.common.exception.WemeetSdkException;
import com.tencentcloudapi.wemeet.models.meeting.QueryUserMeetingsRequest;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboAttachmentEntity;
import com.topscomm.cbo.service.ICboAttachmentService;
import com.topscomm.meet.MeetSystemConst;
import com.topscomm.meet.pojo.MeetRoomManageEntity;
import com.topscomm.meet.utils.TencentMeetUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.tap.common.DatacodeCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.meet.service.IMeetRoomManageService;
import com.topscomm.meet.service.impl.auto.MeetRoomManageServiceAuto;

import java.util.List;
import java.util.Map;

@Service(value = "meetRoomManageService")
@Scope(value = "singleton")
public class MeetRoomManageService extends MeetRoomManageServiceAuto implements IMeetRoomManageService {

    @Autowired
    private ICboAttachmentService cboAttachmentService;
    @Autowired
    private DatacodeCache datacodeCache;


    /**
     * @description: 查询会议室是否已上传图片
     * @author: chengengwei
     * @date: 2021-06-02
     * @modify:
     */
    @Override
    public void addAttachmentState(List<Map<String, Object>> queryRoomList) {
        List<CboAttachmentEntity> cboAttachmentEntities = this.cboAttachmentService
                .queryByWhere("sourcetype = 'meetRoom'");
        queryRoomList.forEach(room -> {
            if (cboAttachmentEntities.stream().anyMatch(item -> item.getSourceid()
                    == ConvertUtil.convertToLong(room.get("id")))) {
                room.put("uploadedpicture", true);
            } else {
                room.put("uploadedpicture", false);
            }
        });
    }
    /**
      * 判断extraParam字段 格式校验 是否为json且包含userid
      * @author xinfushuo
      * @date 2023/4/8
      * @modify
      */
    public boolean checkUseridValid(String extraParam) {
        extraParam = ConvertUtil.convertToString(extraParam);
        //空字符串或者非json类型
        if(extraParam.isEmpty() || !JSONUtil.isJson(extraParam)){
            return false;
        }
        //若为json 需包含userid
        JSONObject extraJson = JSONUtil.parseObj(extraParam);
        if(!extraJson.containsKey("userid")){
            return false;
        }
        String userid = ConvertUtil.convertToString(extraJson.get("userid"));
        QueryUserMeetingsRequest userMeetingsRequest = new QueryUserMeetingsRequest();
        userMeetingsRequest.setPos(0);
        userMeetingsRequest.setUserId(userid);
        userMeetingsRequest.setInstanceId(InstanceEnum.INSTANCE_PC.getInstanceID());
        try{
            TencentMeetUtil.MEETING_CLIENT.queryUserMeetings(userMeetingsRequest);
            return  true;
        }catch (WemeetSdkException exception){
            return false;
        }
    }

    @Override
    protected void beforeInsert(BasicEntity entity) throws ServiceException {
        MeetRoomManageEntity roomManageEntity = (MeetRoomManageEntity) entity;
        if(MeetSystemConst.MeetRoomTypeEnum.Remote.contentEquals(roomManageEntity.getType())&& !checkUseridValid(roomManageEntity.getExtraparam())){
            throw new ServiceException("请正确填写线上会议室参数字段，userid需为实际有效账户id");
        }
    }


    @Override
    protected void updateBefore(Map<String, Object> map) throws ServiceException {
        MeetRoomManageEntity roomManageEntity = new MeetRoomManageEntity();
        roomManageEntity.convertFromMap(map);
        if(MeetSystemConst.MeetRoomTypeEnum.Remote.contentEquals(roomManageEntity.getType())&& !checkUseridValid(roomManageEntity.getExtraparam())){
            throw new ServiceException("请正确填写线上会议室额外参数字段，且userid需为实际有效账户id");
        }
    }
}
